<?php
	class RepositoryConnection{
		protected $connection;
		
		public function __construct(){
			//Suspendet becase ACM blocked the requests to the site
			//if(strpos(RepositoryConnectionProperty::$_url, 'dl.acm.org') !== false){
			//	$this->connection = file_get_html(RepositoryConnectionProperty::$_url);
			if(RepositoryConnectionProperty::$_name == "ACM"){
					$this->connection = $this->googleCustomSearch();
			}elseif(RepositoryConnectionProperty::$_name == "SPRINGERLINK"){
				$this->connection = file_get_html(RepositoryConnectionProperty::$_url);
			}else{
				$this->connection = $this->proxySimplexmlLoadFile(RepositoryConnectionProperty::$_url, RepositoryConnectionProperty::$_proxyName, RepositoryConnectionProperty::$_proxyPort , RepositoryConnectionProperty::$_proxyUsername , RepositoryConnectionProperty::$_proxyPassword );
			}
			
		}
		
		public function __destruct(){
			$this->connection = null;
			/*
			if(strpos(RepositoryConnectionProperty::$_name, 'SPRINGERLINK') !== false){
				$this->connection = null;	
			}else{
				$this->connection = null;
			}
			*/
		}
		
		public function getConnection(){
			return $this->connection;
		}
		
		protected function googleCustomSearch(){
			$client = new apiClient();
			$client->setApplicationName('Google CustomSearch PHP Starter Application');
			// Docs: http://code.google.com/apis/customsearch/v1/using_rest.html
			// Visit https://code.google.com/apis/console?api=customsearch to generate
			// your developer key (simple api key).
			$client->setDeveloperKey('AIzaSyCnyVXlI3vtzXji5Skyq2tjB151GKuxHTM');
			$search = new apiCustomsearchService($client);
				
			return $search;
		}
		
		protected function proxySimplexmlLoadFile($url, $proxy_name, $proxy_port, $proxy_username, $proxy_password) {
		
			global $xml;
			$xml = false;
			$xml_string = '';
			$xml_string2 = '';
		
			if (isset($proxy_name) && !empty($proxy_name)) {
		
				$proxy_fp = @fsockopen($proxy_name, $proxy_port, $errno, $errstr, 10);
		
				if ($proxy_fp) {
		
					fputs($proxy_fp, "GET $url HTTP/1.0\r\nHost: $proxy_name\r\n");
					fputs($proxy_fp, "User-Agent: \"$_SERVER[HTTP_USER_AGENT]\"\r\n");
					fputs($proxy_fp, "Proxy-Authorization: Basic ". base64_encode ("$proxy_username:$proxy_password")."\r\n\r\n");
		
					while(!feof($proxy_fp)) {
						$xml_string2 .= fgets($proxy_fp, 128);
					}
		
					fclose($proxy_fp);
		
					$xml_string = strstr($xml_string2, "<?xml");
					$xml = simplexml_load_string($xml_string);
					#JSTOR hack
					if(empty($xml) && strpos($url, 'jstor') !== false) {
						$xml = new XMLReader();
						$xml->xml($xml_string);
					}
					#NASA PHYS hack
					if (empty($xml) && strpos($url, 'adsabs') !== false) {
		
					$response = array();
					$response = explode("\r\n", $xml_string2);
		
					while(list($key,$value) = each($response)) {
		
					if (stripos($value,"Location: ") === 0) {
					$new_url = trim(substr($value, 10));
					if ($new_url != $url) break;
					}
					}
		
						if (!empty($new_url)) {
		
						$xml_string = '';
		
						if(stripos($new_url, "http") !== 0) $new_url = parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST).$new_url;
		
							$proxy_fp = @fsockopen($proxy_name, $proxy_port);
		
							fputs($proxy_fp, "GET $new_url HTTP/1.0\r\nHost: $proxy_name\r\n");
							fputs($proxy_fp, "User-Agent: \"$_SERVER[HTTP_USER_AGENT]\"\r\n");
							fputs($proxy_fp, "Proxy-Authorization: Basic ". base64_encode ("$proxy_username:$proxy_password")."\r\n\r\n");
		
							while(!feof($proxy_fp)) {
							$xml_string .= fgets($proxy_fp, 128);
						}
		
						fclose($proxy_fp);
		
						$xml_string = strstr($xml_string, "<?xml");
						$xml = simplexml_load_string($xml_string);
						}
						}
						}
		
						} else {
		
						ini_set('user_agent', $_SERVER['HTTP_USER_AGENT']);
							$xml = @simplexml_load_file ($url);
							#JSTOR hack
							if(strpos($url, 'jstor') !== false) {
							$xml = new XMLReader();
							$xml->open($url);
						}
							#NASA PHYS hack
							if (empty($xml) && strpos($url, 'adsabs') !== false) {
							$xml_string2 = '';
							$host = parse_url($url,PHP_URL_HOST);
							$path = parse_url($url,PHP_URL_PATH);
							$query = parse_url($url,PHP_URL_QUERY);
		
							$proxy_fp = @fsockopen($host, 80);
		
							if ($proxy_fp) {
		
								fputs($proxy_fp, "GET $path?$query HTTP/1.0\r\n");
								fputs($proxy_fp, "Host: $host\r\n");
								fputs($proxy_fp, "User-Agent: \"$_SERVER[HTTP_USER_AGENT]\"\r\n\r\n");
		
								while(!feof($proxy_fp)) {
								$xml_string2 .= fgets($proxy_fp, 128);
								}
		
								fclose($proxy_fp);
		
								$response = array();
								$response = explode("\r\n", $xml_string2);
		
								while(list($key,$value) = each($response)) {
		
									if (stripos($value,"Location: ") === 0) {
									$new_url = trim(substr($value, 10));
									if ($new_url != $url) break;
								}
								}
		
								if (!empty($new_url)) {
		
								if(stripos($new_url, "http") !== 0) $new_url = parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST).$new_url;
		
								ini_set('user_agent', $_SERVER['HTTP_USER_AGENT']);
								$xml = @simplexml_load_file ($url);
								}
								}
								}
								}
								if ($xml===false) die ('<div class="ui-state-error ui-corner-all" style="width:300px;text-align:justify;padding:4px;margin:auto;margin-top:10%"><b>Error!</b> I, Librarian could not connect with an external web service.
								This usually indicates that you access the Web through a proxy server. Enter your proxy details in Tools->Settings. Alternatively, the external service may be temporarily down. Try again later.</div>');
								return $xml;
								}
		
	}